package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFSliderWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFSliderWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFSliderWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFSliderWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFSliderWidget widget) {
        ObjectNode ui = node.getUi();
        ui.put("widget", "slider");
        ui.put("range", widget.range());
        if (ArrayUtils.isNotEmpty(widget.marks())) {
            ArrayNode array = putArray(ui, "marks");
            for (String mark : widget.marks()) {
                array.add(mark);
            }
        }
        ui.put("dots", widget.dots());
        putString(ui, "included", widget.included());
        ui.put("vertical", widget.vertical());
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFSliderWidget.class};
    }
}
